package indi.woniu.controller.review;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import indi.woniu.entity.ExamAdministration;
import indi.woniu.entity.ExamAdministrationDTO;
import indi.woniu.entity.ResponseEntity;
import indi.woniu.service.ExamAdministrationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 审核页面(ReviewAdministration)控制层
 *.
 * @author shaoyun
 * @since 2021-12-14 10:30:20
 */

@CrossOrigin
@RestController
@RequestMapping("/reviewAdministration")
public class ReviewAdministrationController {
    /**
     * 服务对象
     */
    @Autowired
    private ExamAdministrationService examAdministrationService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 参数对象
     * @return 单条数据
     */
    @GetMapping("/selectById/{id}")
    public ResponseEntity<ExamAdministration> selectById(@PathVariable Integer id) {
        ExamAdministration result = examAdministrationService.selectById(id);
        if(result != null){
            return new ResponseEntity(200,"查询成功", id);
        }
        return new ResponseEntity(401,"查询失败", id);
    }
    /**
     * 新增一条数据
     *
     * @param examAdministration 实体类
     * @return Response对象
     */
    @PostMapping("/insertOne")
    public ResponseEntity<ExamAdministration> insertOne(@RequestBody ExamAdministration examAdministration) {
        int result = examAdministrationService.insert(examAdministration);
        if (result > 0) {
            return new ResponseEntity(200,"新增成功", examAdministration);
        }
        return new ResponseEntity(401,"新增失败", examAdministration);
    }

    /**
     * 修改一条数据
     *
     * @param examAdministration 实体类
     * @return Response对象
     */
    @PostMapping("/updateOne")
    public ResponseEntity<ExamAdministration> updateOne(@RequestBody ExamAdministration examAdministration) {
//        System.out.println(examAdministration);
        ExamAdministration result = examAdministrationService.updateOne(examAdministration);
        if (result != null) {
            return new ResponseEntity(200,"修改成功", examAdministration.getId());
        }
        return new ResponseEntity(401,"修改失败", examAdministration);
    }

    /**
     * 删除一条数据
     *
     * @param examAdministration 参数对象
     * @return Response对象
     */
    @RequestMapping(value = "/deleteOne/{id}", method = RequestMethod.DELETE)
    public ResponseEntity<ExamAdministration> deleteOne(ExamAdministration examAdministration) {
        int result = examAdministrationService.deleteById(examAdministration.getId());
        if (result > 0) {
            return new ResponseEntity(200,"删除成功", examAdministration);
        }
        return new ResponseEntity(401,"删除失败", examAdministration);
    }

//    /**
//     * 查询全部
//     *
//     * @return Response对象
//     */
//    @RequestMapping(value = "/selectAll", method = RequestMethod.POST)
//    public ResponseEntity<List<ExamAdministration>> selectAll() {
//
//        List<ExamAdministration> examAdministrations = examAdministrationService.selectAll();
//        if (examAdministrations != null) {
//            return new ResponseEntity(200,"查询成功", examAdministrations);
//        }
//        return new ResponseEntity(401,"查询失败", examAdministrations);
//    }

    /**
     * 分页查询
     *
     * @param examAdministrationDTO().getPage 页数
     * @param examAdministrationDTO().getLimit 条数
     * @return Response对象
     */
    @RequestMapping(value = "/selectPage")
    //pageNum 页面数
    //pageSize 每页的数量
    public ResponseEntity<PageInfo<ExamAdministration>> selectPage(@RequestBody ExamAdministrationDTO examAdministrationDTO) {
        PageHelper.startPage(examAdministrationDTO.getPage(), examAdministrationDTO.getLimit());
        List<ExamAdministration> examAdministration = examAdministrationService.selectAllOne(examAdministrationDTO.getExamAdministration());
//        List<ExamAdministration> examAdministration;
//
//
//        if (examAdministrationDTO.getExamAdministration() != null) {
//            examAdministration = examAdministrationService.selectAllTest(examAdministrationDTO.getExamAdministration());
//        }else {
//            examAdministration = examAdministrationService.selectAll();
//        }

        PageInfo<ExamAdministration> examAdministrations = new PageInfo<>(examAdministration);

        if (examAdministrations != null) {
            return new ResponseEntity(200,"查询成功", examAdministrations);
        }
        return new ResponseEntity(401,"查询失败", examAdministrations);
    }

    /**
     * 查询总条数
     *.
     * @return Response对象
     */
    @RequestMapping(value = "/count", method = RequestMethod.GET)
    public int count() {
        int examAdministrations = examAdministrationService.count();

        return  examAdministrations;
    }

}
